<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 示例接口
 */
class Datav extends Api
{


    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['index', 'test','line'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['index', 'test','line'];

    /**
     * datav统计数据
     *
     */
    public function index()
    {
        $today_tel = 348726;
        $all_tel = 56733211;
        $all_user = 1982119;
        $tel_time='20.01';
        $tel_put_through='60.89';
        //中午要停半小时,值不变
        if (get_curr_time_section("12:00:00", "13:29:59")) {
            $today_tel = 510726;
            $all_tel = 56895211;
            $all_user=1992919;
        }

        //  晚上8点到凌晨12点，固定值
        if (get_curr_time_section("20:00:00", "23:59:59")) {
            $today_tel = 892312;
            $all_tel = 57276797;
            $all_user=2090836;
        }

        //  需要计算，读取文件
        if (get_curr_time_section("09:00:00", "11:59:59") || get_curr_time_section("13:30:00", "20:00:00")) {
            // 从文件中读取数据到PHP变量
            $json_string = file_get_contents(ROOT_PATH.'public/datav/'.date("H:i:s").'.json');
            // 把JSON字符串转成PHP数组
            $data = json_decode($json_string, true);
            $today_tel=$data['today_tel'];
            $all_tel=$data['all_tel'];
            $all_user=$data['all_user'];
            $tel_time=(number_format(rand(1912, 2002) / 100, 2));
            $tel_put_through=(number_format(rand(5989, 6189)/100, 2));
        }
        $result[0] = [
            'tel_time' =>$tel_time ,
            'tel_put_through' => $tel_put_through,
            'all_tel' => $all_tel,
            'today_tel' => $today_tel,
            'all_user' => $all_user
        ];

        return json_encode($result);
    }




    /**
     * datav 折线图数据
     *
     */
    public function line()
    {
        $line=json_decode('[491312,504642,517973,531304,544634,557964,571294,584625,597956,611287,624618,637949,651280,664610,677941,691272,704602,717933,731264,744595,757926,771256,784587,797918,811248,824579,837910,851241,864571,877902,892312]');
        $data=[];
        $i=5;

        for ($start = strtotime(date('y-m-d 00:00:00',strtotime('-30 days'))); $start <=strtotime(date('y-m-d 00:00:00',time())); $start+=518400) {

            $data[]=[
                'x'=>date('m/d', $start),
                'y'=>$line[$i]
            ];
            $i+=5;
        }
        return json_encode($data);
    }





    public function test()
    {
//        $today = 348726;
//        $all = 56733211;
//        $all_user = 1982119;
//        $arr = [];
//        for ($a = 0; $a <= 34200; $a++) {
//            $b = round(15, 19);
//            $c = round(3, 5);
//
//            $today += $b;
//            $all += $b;
//            $all_user += $c;
//            $arr[] = [
//                "today_tel" => $today,
//                "all_tel" => $all,
//                "all_user" => $all_user
//            ];
//        }
//
//        echo "结构创建完成";
//
//
//// 生成一个PHP数组
//        $data = array();
//        $data[0] = array('1', '吴者然', 'onestopweb.cn');
//        $data[1] = array('2', '何开', 'iteye.com');
//// 把PHP数组转成JSON字符串
//        $json_string = json_encode($data);
//// 写入文件
//        $starttime = "2012-08-13 13:30:00";
//        $endtime = "2012-08-13 20:00:00";
//        $i = 10800;
//        for ($start = strtotime($starttime); $start <= strtotime($endtime); $start++) {
//
////    print_r(date('H:i:s', $start));
//            file_put_contents('/Users/furlong/phpweb/wanxun/vxrobot/public/datav/' . date('H:i:s', $start) . '.json', json_encode($arr[$i]));
//
//            $i++;
//            echo $i . "\r\n";
//        }
//

    }





}